﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

// 版权所有：2014  All Rights Reserved.
// 文 件 名：NotifyIconHelper.cs
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2014/12/6 14:10:04
// 修改描述：
//----------------------------------------------------------------*/
namespace MslDiary.Utils
{
    public class NotifyIconHelper
    {
        private static NotifyIconHelper instance = null;
        public static NotifyIcon NotifyIcon = new NotifyIcon();

        #region 单例

        private NotifyIconHelper()
        {
            SetupNotifyIcon();
        }
        public static NotifyIconHelper Instance()
        {
            if (instance == null)
            {
                instance = new NotifyIconHelper();
            }
            return instance;
        }
        #endregion

        #region 提示信息

        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="text"></param>
        public void ShowBalloonTip(string title, string text)
        {
            if (String.IsNullOrEmpty(text))
                return;
            try
            {
                NotifyIcon.BalloonTipTitle = title;
                NotifyIcon.BalloonTipText = text;
                NotifyIcon.BalloonTipIcon = ToolTipIcon.None;

                NotifyIcon.ShowBalloonTip(5000);
            }
            catch (Exception ex)
            {
                MSL.Tool.LogHelper.Instance.WriteLog("ShowBalloonTip BalloonTipTitle:[" + title + "]\r\nBalloonTipText:[" + text + "]\r\n\r\n" + ex.ToString());
            }
        }
        #endregion

        #region 托盘菜单

        /// <summary>
        /// 托盘菜单
        /// </summary>
        private void SetupNotifyIcon()
        {
            NotifyIcon.Icon = MslDiary.Properties.Resources.logo;
            NotifyIcon.ContextMenuStrip = new ContextMenuStrip();

            ToolStripSeparator separatorA = new ToolStripSeparator();//分隔线
            ToolStripMenuItem homeMI = new ToolStripMenuItem("访问主页") { };
            homeMI.Image = MslDiary.Properties.Resources.home_page;
            homeMI.Click += (s, e) =>
            {
                System.Diagnostics.Process.Start(MslDiary.Framework.Gloable.HomePageUrl);
            };

            ToolStripMenuItem exitMI = new ToolStripMenuItem("退出");
            exitMI.Click += (s, e) =>
            {
               MslDiary.Utils.Helper.Instance.AlertConfirm(null, "确定要退出托盘应用程序吗？", () =>
                {
                    Helper.Instance.ExitApp();
                    NotifyIconHelper.Instance().Hide();
                });
            };

            NotifyIcon.ContextMenuStrip.Items.AddRange(new ToolStripItem[] { homeMI, separatorA, exitMI });
            NotifyIcon.Visible = true;
        }
        #endregion

        // 隐藏
        public void Hide()
        {
            NotifyIcon.Visible = false;
        }
    }
}
